# MD5（13位时间戳+8位十进制随机数字+版本号+signKey）后转16进制大写字符串
# 注意：sign拼接顺序不能改变，以下为示例
# var sign = MD5(timestamp + nonce + version + signKey).toUpperCase()

import string
import hashlib
import random
import time
import requests
from lib.tools import cxt_myRequest

testAdress = 'http://apiishop.tt.sanwenqian.cn'

timeStamp = str(int(round(time.time()*1000)))

nonce = ''.join(random.sample(string.digits, 8))

version = '1.0'
signKey = 'PB7L5K8CUCLIYRALDR813SRP6BKSPNXQ'

token = 'nkAtL9zAljRzqq58rgGuaA=='

str = timeStamp+nonce+version+signKey

def md5forsign(str):
    md = hashlib.md5()
    md.update(str.encode())
    res = md.hexdigest().upper()
    return res

sign = md5forsign(str)

print(timeStamp, nonce, sign)

# 发送验证码
# sendCode = testAdress+'/Z_Api/v1/login/send'
#
# data = {
#   "sign": sign,
#   "nonce": nonce,
#   "version": version,
#   "timestamp": timeStamp,
#   "fields": {
#     "mobile": "17610769836"
#   }
# }
#
# sendMessage = requests.post(sendCode, json=data).json()
# print(sendMessage)

# 确认登录
# url_login = testAdress + '/Z_Api/v1/login/confirm'
#
# data_login = {
#   "sign": sign,
#   "nonce": nonce,
#   "version": version,
#   "timestamp": timeStamp,
#   "fields": {
#      "mobile":"17610769836",
#      "dynamic":"294110"
#   }
# }
#
#
# loginRes = requests.post(url_login, json=data_login).json()
# print(loginRes)



def publicParas(fields):
  postData = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "fields": fields
}
  return postData

# 4-2商品模块
url_query = testAdress + '/Z_Api/v1/goods/query'

# data_query = {
#   "sign": sign,
#   "nonce": nonce,
#   "version": version,
#   "timestamp": timeStamp,
#   "token": token,
#   "fields": {
#     "page":1,
#     "pageSize":10
#   }
# }
data_f = {
    "page":1,
    "pageSize":10
  }

data_query = publicParas(data_f)

queryRes = requests.post(url_query, json=data_query).json()
print('++++++++++', queryRes)

res = cxt_myRequest('post', url_query, data_query)

# 商品详情
url_goodsget = testAdress + '/Z_Api/v1/goods/get'
data_goodsget = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "fields": {
    "goodsId":15
  }
}

goodsgetRes = requests.post(url_goodsget, json=data_goodsget).json()
print(goodsgetRes)

# 商品兑换

url_goodswap = testAdress + '/Z_Api/v1/goods/swap'
data_goodswap = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "fields": {
    "goodsId":15
  }
}

goodswapRes = requests.post(url_goodswap, json=data_goodswap).json()
print(goodswapRes)

# 检测APP更新

url_check = testAdress + '/Z_Api/v1/version/check'
data_check = {
  "sign": sign,
  "nonce": nonce,
  "version": version,
  "timestamp": timeStamp,
  "token": token,
  "fields": {
    "platform": 0
  }
}

checkRes = requests.post(url_check, json=data_check).json()
print(checkRes)



